﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIManager {
    private Dictionary<UIPanelType, string> uiDictionary;
    private Dictionary<UIPanelType, GameObject> prefabDict;
    private Stack<GameObject> panelStack;
    private static UIManager _instance = null;
    private Transform canvasTransform;
    private Transform CanvasTransform{
        get{
            if(canvasTransform == null){
                canvasTransform = GameObject.Find("Canvas").transform;
            }
            return canvasTransform;
        }
        set{
            canvasTransform = value;
        }
    }
    /*
     *单例
     */
    public static UIManager Instance
    {
        get{
            if (_instance == null)
            {
                _instance = new UIManager();
            }
            return _instance;
        }
        
    }
    private UIManager()
    {
        init();
        LoadUIInfo();
    }
    private void init()
    {
        //预制字典 存放所有预制避免重复加载
        prefabDict = new Dictionary<UIPanelType, GameObject>();
        uiDictionary = new Dictionary<UIPanelType, string>();
        panelStack = new Stack<GameObject>();
    }
    /*
     * 用于json解析暂存数据
     * panelInfos 这个名字要跟json数据里面的名字对应
     */
    class PanelInfoObject
    {
        public List<UIPanelInfo> panelInfos = null;
    }
    private void LoadUIInfo()
    {
        TextAsset ta = Resources.Load<TextAsset>("UIPanelType");
        //C# json只能解析对象类型 ["1","2","3","4"] 要写成 {"infos":["1","2","3","4"]}
        PanelInfoObject panelObject = JsonUtility.FromJson<PanelInfoObject>(ta.text);
        foreach (UIPanelInfo info in panelObject.panelInfos)
        {
            uiDictionary.Add(info.panelType,info.Path);
        }
    }
    private GameObject getPanel(UIPanelType panelType)
    {
        GameObject prefab = getPrefab(panelType);
        GameObject basePanel = GameObject.Instantiate(prefab);
        return basePanel;
    }
    private GameObject getPrefab(UIPanelType panelType)
    {
        GameObject prefab = prefabDict.GetValue(panelType,null);
        if (prefab == null)
        {
            string path = uiDictionary.GetValue(panelType);
            prefab = Resources.Load(path) as GameObject;
            prefabDict.TryAdd(panelType, prefab);
        }
        return prefab;
    }
    public GameObject pushPanel(UIPanelType panelType)
    {
        GameObject panel = getPanel(panelType);
        panel.transform.SetParent(CanvasTransform,false);
        if (panelStack.Count > 0)
        {
            GameObject topPanel = panelStack.Peek();
            topPanel.SendMessage("OnPause",SendMessageOptions.DontRequireReceiver);
        }
        panel.SendMessage("OnEnter",SendMessageOptions.DontRequireReceiver);
        panelStack.Push(panel);
        return panel;
    }
    public void popPanel()
    {
        if (panelStack.Count > 0)
        {
            GameObject topPanel = panelStack.Pop();
            topPanel.SendMessage("OnExit", SendMessageOptions.DontRequireReceiver);
            GameObject.Destroy(topPanel);
            if (panelStack.Count > 0)
            {
                topPanel = panelStack.Peek();
                topPanel.SendMessage("OnResume", SendMessageOptions.DontRequireReceiver);
            }
        }
    }

}
